package com.yingxin.yxerp.module.sys.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yingxin.yx.framework.commons.dto.PageOut;
import com.yingxin.yx.framework.mybatisplus.PageHelper;
import com.yingxin.yxerp.module.sys.entity.BankAccount;
import com.yingxin.yxerp.module.sys.entity.builder.BankAccountBuilder;
import com.yingxin.yxerp.module.sys.service.IBankAccountService;
import com.yingxin.yxerp.module.sys.vo.req.BankAccountCreateVo;
import com.yingxin.yxerp.module.sys.vo.req.BankAccountQueryVo;
import com.yingxin.yxerp.module.sys.vo.req.BankAccountUpdateVo;
import com.yingxin.yxerp.module.sys.vo.resp.BankAccountRespVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 银行账户 前端控制器
 * @author fanchao
 * @since 2021-08-18
 */

@RestController
@RequestMapping("/finance/bank-account")
public class BankAccountController {

    @Autowired
    private IBankAccountService bankAccountService;


    @ApiOperation(value = "查询列表（分页）")
    @PostMapping("/page")
    public PageOut<BankAccountRespVo> page(@RequestBody BankAccountQueryVo queryVo) {
        IPage<BankAccountRespVo> page = bankAccountService.pageVo(PageHelper.in(queryVo), queryVo);
        return PageHelper.out(page);
    }

    @ApiOperation(value = "查询列表（全部）")
    @PostMapping("/list")
    public List<BankAccountRespVo> list(@RequestBody BankAccountQueryVo queryVo) {
        return bankAccountService.listVo(queryVo);
    }

    @ApiOperation(value = "根据id查询")
    @GetMapping("/{id}")
    public BankAccountRespVo get(@PathVariable("id") String id) {
        return bankAccountService.getVo(id);
    }


    @PreAuthorize("hasAuthority('bank:account:add')")
    @ApiOperation(value = "新增")
    @PostMapping
    public String create(@RequestBody @Valid BankAccountCreateVo createVo) {
        BankAccount account = BankAccountBuilder.build(createVo);
        bankAccountService.create(account);
        return account.getId();
    }

    @PreAuthorize("hasAuthority('bank:account:update')")
    @ApiOperation(value = "修改")
    @PutMapping
    public String update(@RequestBody @Valid BankAccountUpdateVo updateVo) {
        BankAccount account = BankAccountBuilder.build(updateVo);
        bankAccountService.update(account);
        return account.getId();
    }

    @PreAuthorize("hasAuthority('bank:account:del')")
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public void delete(@PathVariable("id") String id) {
        BankAccount account = bankAccountService.getById(id);
        bankAccountService.delete(account);
    }

}